package com.hutao.manage.util;

import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:DownFileUtil
 * Package：com.slmr.archive.util
 * Description:
 *
 * @Date:2020-09-18 14:29
 * @Author:liyutao
 */
public class DownFileUtil {

    public static String downFileByUrl(String urlList,String targetFilePath){
        URL url = null;
        String fileName = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            //fileName = StringUtil.getFileName()+".jpg";

            FileOutputStream fileOutputStream = new FileOutputStream(new File(targetFilePath));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            BASE64Encoder encoder = new BASE64Encoder();
            String encode = encoder.encode(buffer);//返回Base64编码过的字节数组字符串
            System.out.println(encode);
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            dataInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }


    public static String downFileByUrl(String url,String[] targetFilePathArr){
        String fileName = StringUtil.getFileName()+".jpg";
        for (String targetFilePath:targetFilePathArr) {
           downFileByUrl(url,targetFilePath+fileName);
        }
        return fileName;

    }


	/*
	 * public static void main(String[] args) { String [] folderArr =
	 * {"D://test/thumbil/","D://test/thumbil_/","D://test/source/"}; String
	 * fileName = downFileByUrl(
	 * "http://localhost:8090/upload/ranrs2002/2-09999646/582eba110d2148b19a8b.jpg"
	 * ,folderArr);
	 * 
	 * System.out.println(fileName); }
	 */
}
